{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### MinMaxScaler\n",
    "\n",
    "MinMaxScaler 是一种数据预处理方法，用于将特征缩放到指定的范围（通常为 [0, 1]）。它通过线性变换将数据映射到给定区间，适用于需要将数据限制在特定范围的场景。\n",
    "\n",
    "Xscaled = ( X - Xmin ) / ( Xmax - Xmin )\n",
    "\n",
    "默认情况下，数据被缩放到 [0, 1]，但可以通过 feature_range 参数调整范围。\n",
    "\n",
    "fit(X): 计算数据的最小值和最大值。\n",
    "\n",
    "transform(X): 根据计算的最小值和最大值缩放数据。\n",
    "\n",
    "fit_transform(X): 先拟合数据，再进行缩放"
   ],
   "id": "d7dd54ef0b60b558"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-13T15:15:19.401673Z",
     "start_time": "2025-01-13T15:15:17.620152Z"
    }
   },
   "source": [
    "from sklearn.preprocessing import MinMaxScaler\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T15:15:19.407983Z",
     "start_time": "2025-01-13T15:15:19.401673Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 示例数据\n",
    "data = np.array([[1, 2], [2, 3], [3, 4]])\n",
    "# 创建 MinMaxScaler 对象\n",
    "scaler = MinMaxScaler(feature_range=(0, 1))\n",
    "# 拟合并转换数据\n",
    "scaled_data = scaler.fit_transform(data)\n",
    "print(\"原始数据:\\n\", data)\n",
    "print(\"缩放后的数据:\\n\", scaled_data)"
   ],
   "id": "3bf2baad5fb54a80",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据:\n",
      " [[1 2]\n",
      " [2 3]\n",
      " [3 4]]\n",
      "缩放后的数据:\n",
      " [[0.  0. ]\n",
      " [0.5 0.5]\n",
      " [1.  1. ]]\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### StandardScaler\n",
    "\n",
    "StandardScaler 是一种数据标准化方法，通过将特征缩放到均值为 0、标准差为 1 的标准正态分布来处理数据。它适用于许多机器学习算法，尤其是那些假设数据服从正态分布或对特征尺度敏感的算法（如 SVM、线性回归、KNN 等）。\n",
    "\n",
    "StandardScaler 对每个特征进行以下变换：\n",
    "\n",
    "Xscaled = ( X - μ )/ σ\n",
    "\n",
    "X 是原始数据。\n",
    "\n",
    "μ 是特征的均值。\n",
    "\n",
    "σ 是特征的标准差。\n",
    "\n",
    "标准化后的数据服从期望为 0，标准差为 1的均匀分布。\n",
    "\n",
    "相关参数：\n",
    "\n",
    "with_mean: 是否将数据居中（均值为 0），默认为 True。\n",
    "\n",
    "with_std: 是否将数据缩放到单位方差（标准差为 1），默认为 True。\n",
    "\n",
    "copy: 是否复制数据，默认为 True。\n",
    "\n",
    "主要方法：\n",
    "\n",
    "fit(X): 计算数据的均值和标准差。\n",
    "\n",
    "transform(X): 根据计算的均值和标准差标准化数据。\n",
    "\n",
    "fit_transform(X): 先拟合数据，再进行标准化。\n",
    "\n",
    "inverse_transform(X): 将标准化后的数据还原为原始数据。"
   ],
   "id": "200088e52730ed21"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T15:15:19.411865Z",
     "start_time": "2025-01-13T15:15:19.407983Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "import numpy as np"
   ],
   "id": "305d5b55e1d74f0c",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T15:15:25.789448Z",
     "start_time": "2025-01-13T15:15:25.768754Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 示例数据\n",
    "data = np.array([[1, 2], [2, 3], [3, 4]])\n",
    "# 创建 StandardScaler 对象\n",
    "scaler = StandardScaler()\n",
    "# 拟合并转换数据\n",
    "scaled_data = scaler.fit_transform(data)\n",
    "print(\"原始数据:\\n\", data)\n",
    "print(\"标准化后的数据:\\n\",scaled_data)"
   ],
   "id": "3bdd6b1694158023",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据:\n",
      " [[1 2]\n",
      " [2 3]\n",
      " [3 4]]\n",
      "标准化后的数据:\n",
      " [[-1.22474487 -1.22474487]\n",
      " [ 0.          0.        ]\n",
      " [ 1.22474487  1.22474487]]\n"
     ]
    }
   ],
   "execution_count": 4
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
